package com.zjw.zy.bean;

import android.os.Parcel;
import android.os.Parcelable;

import androidx.annotation.Nullable;

import com.google.gson.annotations.SerializedName;
import com.zjw.zy.utils.GsonUtils;
import com.zjw.zy.utils.LogUtils;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * @author ：zhong.jw
 * @date ：Created in 2022/12/8 9:15
 */
public class User implements Parcelable {

    public static final String BIND_INFO_KEY = "BIND_INFO_KEY";


    @SerializedName("username")
    private String username;

    @SerializedName("love_username")
    private String loveUsername;

    @SerializedName("info")
    private String info;

    private String address;

    private String name;


    public User() {
    }


    protected User(Parcel in) {
        username = in.readString();
        loveUsername = in.readString();
        info = in.readString();
        address = in.readString();
        name = in.readString();
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(username);
        dest.writeString(loveUsername);
        dest.writeString(info);
        dest.writeString(address);
        dest.writeString(name);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public static final Creator<User> CREATOR = new Creator<User>() {
        @Override
        public User createFromParcel(Parcel in) {
            return new User(in);
        }

        @Override
        public User[] newArray(int size) {
            return new User[size];
        }
    };

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getLoveUsername() {
        return loveUsername;
    }

    public void setLoveUsername(String loveUsername) {
        this.loveUsername = loveUsername;
    }

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    public void putInfo(String key, Object payload) {
        try {
            JSONObject object = new JSONObject(info == null ? "{}" : info);
            object.put(key, GsonUtils.toJson(payload));
            info = object.toString();
        } catch (JSONException e) {
            LogUtils.e("user", e.getMessage());
        }
    }

    @Nullable
    public <T> T getInfo(String key, Class<T> clazz) {
        try {
            JSONObject object = new JSONObject(info == null ? "{}" : info);
            String opt = object.optString(key);
            return GsonUtils.fromJson(opt, clazz);
        } catch (JSONException e) {
            LogUtils.e("user", e.getMessage());
        }
        return null;
    }
}
